package ru.spbau.database.group3.parser;

import ru.spbau.database.group3.queryEvaluator.Instruction;
import ru.spbau.database.group3.queryEvaluator.ErrorInstruction;
import ru.spbau.database.group3.queryEvaluator.UpdateTableInstruction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: stas
 * Date: 1/30/13
 * Time: 3:32 PM
 */
public class UpdateTableCmd extends abstractCmd {

    public UpdateTableCmd() {
        super(StringConstants.UPDATE);
    }

    @Override
    public Instruction process(String cmd) throws IOException {
        try {
            String tableName = getTableName(cmd);
            List<String> cols = getCols(cmd);
            List<Object> values = getValues(cmd);
            List<Constraint> constraints = getConstraints(cmd);
            return new UpdateTableInstruction(tableName, cols, values, constraints);
        } catch (IndexOutOfBoundsException e) {
            return new ErrorInstruction(e.getMessage());
        } catch (IllegalArgumentException e) {
            return new ErrorInstruction(e.getMessage());
        }
    }

    private List<Constraint> getConstraints(String cmd) {
        int begin = cmd.indexOf(StringConstants.WHERE);
        if(begin > 0) {
            return parseConstraintString(cmd.substring(begin + StringConstants.WHERE.length()).trim());
        }
        return new ArrayList<Constraint>();
    }

    private List<Constraint> parseConstraintString(String cmd) {
        List<Constraint> constraintList = new ArrayList<Constraint>();
        String[] constraints = cmd.split("AND");
        for(String cs : constraints) {
            constraintList.add(RecordConstraint.createConstraint(cs.trim()));
        }
        return constraintList;
    }

    private List<Object> getValues(String cmd) {
        int begin = cmd.indexOf(StringConstants.SET) + StringConstants.SET.length();
        int end = cmd.indexOf(StringConstants.WHERE);
        String[] col_val = cmd.substring(begin, end).trim().split(",");
        ArrayList<Object> values = new ArrayList<Object>();
        for(String s : col_val) {
            values.add(getValue(s.split("=")[1].trim()));
        }
        return values;
    }

    private Object getValue(String value) {
        if(value.matches("\".*\"")) {
            return value.substring(1, value.length()-1);
        } else {
            if(value.indexOf('.') > 0) {
                return Double.parseDouble(value);
            } else {
                return Integer.parseInt(value);
            }
        }
    }

    private List<String> getCols(String cmd) {
        int begin = cmd.indexOf(StringConstants.SET) + StringConstants.SET.length();
        int end = cmd.indexOf(StringConstants.WHERE);
        String[] col_val = cmd.substring(begin, end).trim().split(",");
        ArrayList<String> cols = new ArrayList<String>();
        for(String s : col_val) {
            cols.add(s.split("=")[0].trim());
        }
        return cols;
    }

    private String getTableName(String cmd) {
        int begin = StringConstants.UPDATE.length();
        int end = cmd.indexOf(StringConstants.SET);
        return cmd.substring(begin, end).trim();
    }




}
